package vg.modules.exporters;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import vg.services.data_base_manager.data.header.GraphModelHeader;
import vg.services.main_manager.MainManager;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AMenuItem;
import vg.shared.exporters.GraphMLExporter;

public class GraphMLExporterPlugin implements IPlugin {
	private static final String DEF_LAST_GRAPHML_EXPORT_DIRECTORY = "DEF_LAST_GRAPHML_EXPORT_DIRECTORY";
	
	/**
	 * Constructor.
	 * @param userInterface - feedback between this plugin and system.
	 */
	public void install(final PluginParameter pluginParameter) throws Exception {
		// initialize components
		JMenuItem export2GraphMLMenuItem = new JMenuItem("Export to GraphML"); 

		// initialize listeners
		export2GraphMLMenuItem.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				List<Integer> model_ids = pluginParameter.model.getGraphModelIds();
				
				if (model_ids.size() > 0) {
					// selecting graph model
					List<GraphModelHeader> model_headers = new ArrayList<GraphModelHeader>();
					String[] possibilities = new String[model_ids.size()];
					
					int i = 0;
					for (Integer buf : model_ids) {
						GraphModelHeader model_header = pluginParameter.model.getGraphModelHeader(buf);
						model_headers.add(model_header);
						possibilities[i++] = model_header.name;
					}
	
					Object graph_model_name = (String) JOptionPane.showInputDialog(
									pluginParameter.userInterface.getMainFrame(),
									"Select graph model:", "Select dialog",
									JOptionPane.PLAIN_MESSAGE, null,
									possibilities, possibilities[0]);
					
					// getting graph model id
					GraphModelHeader selecting_model_header = null;
					for (GraphModelHeader buf : model_headers) {
						if (buf.name.equals(graph_model_name)) {
							selecting_model_header = buf;
							break;
						}
					}
					// export GraphML
					if (selecting_model_header != null) {
						try {
							JFileChooser fileChooserDialog = new JFileChooser();
							fileChooserDialog.setFileFilter(new FileFilter() {
								public String getDescription() {				
									return "GraphML files(*.graphml)";
								}
								public boolean accept(File f) {
									if (f != null) {
										if (f.isDirectory())
											return true;
										return f.getName().toLowerCase().endsWith("graphml");
									}
									return false;
								}
							});
							fileChooserDialog.setAcceptAllFileFilterUsed(false);
							String lastDir = MainManager.config.getProperty(DEF_LAST_GRAPHML_EXPORT_DIRECTORY);
							if(lastDir != null) {
								fileChooserDialog.setCurrentDirectory(new File(lastDir));
							}
							Component parent = MainManager.view.getMainFrame();
							if(fileChooserDialog.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
								MainManager.config.setProperty(DEF_LAST_GRAPHML_EXPORT_DIRECTORY, fileChooserDialog.getCurrentDirectory().getAbsolutePath());
								File saveFile = fileChooserDialog.getSelectedFile();
								String fileName = saveFile.getAbsolutePath() + ".graphml";
								GraphMLExporter.saveGraphML(fileName, selecting_model_header.id);
							}
						} catch (Exception ex) {
							MainManager.windowMessage.showExceptionMessage(ex);
						}
					}
				} else {
					MainManager.windowMessage.warningMessage("No graph models", "Warning");
				}
			}
		});
		
		pluginParameter.userInterface.addMenuItem(new AMenuItem(export2GraphMLMenuItem) {
			public void update(Observable o, Object arg) {}
		}, "Export");				
	}
}
